package com.example.demo.config;

import com.example.demo.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统拦截器
 * 1.通过token获取userId
 * 2.接口加密校验
 *
 * @author tandk
 * @date 2017/3/7 0007
 */
@Slf4j
public class SystemInterceptor implements HandlerInterceptor {


    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        log.info("【{}_{}请求_Start】{}", HttpUtil.getRemoteHost(request), request.getMethod(), HttpUtil.getFullUrl(request));
        //1.通过token获取userId
        String url = request.getRequestURL().toString();
//        if (request.getParameter("name").equals("王八")) {
//            HttpUtil.printFailMessage(response, new BaseResult(300, "名字太诡异"));
//            return false;
//        }

        //2.接口加密校验
//        String saltVal = map.get("key").toString();
//        if(!MD5Util.sortAndMd5ForParams(saltVal,request)){
//            HttpUtil.printFailMessage(response, new BaseResult(MerchantConstants.RESPONSE_CODE_401, MerchantConstants.RESPONSE_MESSAGE_401));
//            return false;
//        }
        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
